#include "gamehistorymodel.h"

GameHistoryModel::GameHistoryModel(QObject *parent)
    : QAbstractListModel{parent}
{}

int GameHistoryModel::rowCount(const QModelIndex &parent) const {
    return m_data.count();
}

QVariant GameHistoryModel::data(const QModelIndex &index, int role) const {
    if (!index.isValid())
        return QVariant();
    switch(role) {
    case HeroIDRole:
        return QVariant(m_data[index.row()].hero);
    case DateRole:
        return QVariant(m_data[index.row()].getDateString());
    case ResultRole:
        return QVariant(m_data[index.row()].result);
    case KDARole:
        return QVariant(m_data[index.row()].getKDAString());
    default:
        return QVariant();
    }
}

QHash<int, QByteArray> GameHistoryModel::roleNames() const {
    QHash<int, QByteArray> roles;
    roles[HeroIDRole] = "hrid";
    roles[DateRole] =   "date";
    roles[ResultRole] = "rslt";
    roles[KDARole] =    "kda";
    return roles;
}

void GameHistoryModel::reset(QList<GameHistory> data) {
    emit beginResetModel();
    qDebug() << "GameHistroyModel reset " << data.size();
    for (int i = 0; i < data.size(); i++) {
        qDebug() << data[i].hero << "|" << data[i].result << "|" << data[i].date << "|" << data[i].kill << "|" << data[i].death << "|" << data[i].assist;
    }
    m_data = data;
    emit endResetModel();
}
